package servlet;

import utils.MyDBUtil;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet(name = "InsertServlet")
public class InsertServlet extends HttpServlet {


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sql = "INSERT INTO grade (studentid,quiz,exam) VALUES(?,?,?)";

        PreparedStatement ps = null;

        Connection conn = MyDBUtil.connetDB(); // get db connection
        int result = 0;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, "b63030007");
            ps.setInt(2, 99);
            ps.setInt(3, 99);
            result = ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            MyDBUtil.closeDB();
        }

        String message = null;

        if(result == 0){
            message = "INSERT FAIL";
        }else{
            message = "INSERT SUCCESS";
        }

        request.setAttribute("message", message);
        RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
        rd.forward(request, response);
    }
}
